// pages/myFavourite/myFavourite.js
const db = wx.cloud.database();
const app = getApp();
const httpUrl = "https://6d61-manbu-2ggvwdnk1d8bd0fa-1316354189.tcb.qcloud.la/"
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0';
var diaryArr = [];
var contextColor = {
    "愉悦": "rgba(250, 226, 192, 1);",
    "轻松": "rgba(253, 243, 192, 1);",
    "无语": "rgba(213, 236, 208, 1);",
    "发愁": "rgba(217, 217, 240, 1);",
    "崩溃": "rgba(188, 201, 207, 1);",
}
var contextEmoji = {
    "愉悦": "../../images/laughing.png",
    "轻松": "../../images/smile.png",
    "无语": "../../images/sighing.png",
    "发愁": "../../images/crying.png",
    "崩溃": "../../images/angry.png"
}
var deepThemeColorArr = {
    "愉悦": "#FFA638",
    "轻松": "#FBEE69",
    "无语": "#ADDB89",
    "发愁": "#9AA8CB",
    "崩溃": "#373526",
    "": "#FFFFFF"
}
Page({

    /**
     * 页面的初始数据
     */
    data: {
        contextColor: contextColor,
        contextEmoji: contextEmoji,
        deepThemeColorArr: deepThemeColorArr
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getFavourite();
    },
    getFavourite(){
        let that = this;
        db.collection("infoDB").where({
            _openid: app.globalData.openid
        }).get({
            success: function(res) {
                let favourite = Object.keys(res.data[0].favourite);
                console.log(favourite);
                that.setData({
                    
                });
                that.diaryRefresh(favourite);
            }
        });
    },
    diaryRefresh(favourite){
        let that = this;
        db.collection("diaryDB").where({
            _id: db.command.in(favourite) 
        }).get({
            success: function(res) {
                diaryArr = res.data;
                for ( let i in diaryArr ) {
                    let images = [];
                    for ( let j in diaryArr[i]["cloudPath"] ) {
                        let url = diaryArr[i]["cloudPath"][j];
                        let image = httpUrl + url.substr(70,url.length);
                        images.push(image);
                    }
                    diaryArr[i]["images"] = images;
                    diaryArr[i]["timeString"] = diaryArr[i].createTime.valueOf();
                    db.collection("infoDB").where({
                        _openid: diaryArr[i]["_openid"]
                    }).get({
                        success: function(res){
                            if ( res.data.length == 0 ) {
                                diaryArr[i]["nickname"] = "微信用户";
                                diaryArr[i]["avatar"] = defaultAvatarUrl;
                            }
                            else {
                                diaryArr[i]["nickname"] = res.data[0].nickname;
                                if ( res.data[0].avatar )
                                    diaryArr[i]["avatar"] = that.loadAvatar(res.data[0].avatar);
                                else
                                    diaryArr[i]["avatar"] = defaultAvatarUrl;
                            }
                            that.setData({
                                diaryArr: diaryArr
                            });
                        }
                    })

                    diaryArr[i]["likeNumber"] = Object.keys(diaryArr[i]["like"]).length;
                    diaryArr[i]["favouriteNumber"] = Object.keys(diaryArr[i]["favourite"]).length;
                    let maxLikeIndex = 0;
                    for ( let j in diaryArr[i]["comment"] ) {
                        let likeNumber = Object.keys(diaryArr[i]["comment"][j]["like"]).length;
                        if ( likeNumber >= Object.keys(diaryArr[i]["comment"][maxLikeIndex]["like"]).length )
                            maxLikeIndex = j;
                    }
                    if ( diaryArr[i]["comment"].length > 0 ) {
                        db.collection("infoDB").where({
                        _openid: diaryArr[i]["comment"][maxLikeIndex]["openid"]
                        }).get({
                        success: function(res){
                            if ( res.data.length == 0 ) {
                                diaryArr[i]["hotCommentNickname"] = "微信用户";
                                diaryArr[i]["hotCommentAvatar"] = defaultAvatarUrl;
                                diaryArr[i]["comment"][maxLikeIndex]["comment"];
                            }
                            else {
                                diaryArr[i]["hotCommentNickname"] = res.data[0].nickname;
                                if ( res.data[0].avatar )
                                    diaryArr[i]["hotCommentAvatar"] = that.loadAvatar(res.data[0].avatar);
                                else
                                    diaryArr[i]["hotCommentAvatar"] = defaultAvatarUrl;
                                diaryArr[i]["hotComment"] = diaryArr[i]["comment"][maxLikeIndex]["comment"];
                            }
                            that.setData({
                                diaryArr: diaryArr
                            });
                        }
                        });
                    }

                }
                that.setData({
                    diaryArr: res.data
                })
            }
        });
    },
    fixDiary(e){
        let id = e.currentTarget.dataset.id;
        let year = e.currentTarget.dataset.year;
        let month = e.currentTarget.dataset.month;
        let day = e.currentTarget.dataset.day;
        let emoji = e.currentTarget.dataset.emoji;
        let diary = e.currentTarget.dataset.diary;
        let images = e.currentTarget.dataset.images;
        let authorid = e.currentTarget.dataset.authorid;
        let cloudPath = e.currentTarget.dataset.cloudimage;
        let timeString = e.currentTarget.dataset.timestring;
        db.collection("diaryDB").doc(id).update({
            data: {
                hot: db.command.inc(1)
            }
        })
        wx.navigateTo({
            url: '../diary/diary',
            success: function(res) {
                res.eventChannel.emit('acceptDataFromOpenerPage',{year:year,month:month,authorid:authorid,day:day,emoji:emoji,diary:diary,id:id,images:images,cloudimage:cloudPath,publish:false,fixmode: false,visit:true,timeString:timeString});
            }
        })
    },
    gotoSpace(e){
        let authorid = e.currentTarget.dataset.authorid;
        wx.navigateTo({
            url: "../space/space",
            success: function(res) {
                res.eventChannel.emit('acceptDataFromOpenerPage',{otherid: authorid});
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },
    loadAvatar(url){
        let file = url.substr(70,url.length);
        return httpUrl + file;
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})